from fastapi import APIRouter, Depends
from ..models import UserInfo, Job
from ..schemas import JobInSchema, JobOutSchema
from src.utils.common_response import APIResponse
from typing import List

router = APIRouter()


@router.get("/jobs", description='查询所有岗位')
async def het_job_list():
    jobs = await Job.filter(is_delete=False,pid=None).all().prefetch_related('children')
    job_dict = [await JobOutSchema.from_orm_recursive(job) for job in jobs]
    return APIResponse(msg='查询成功', result=job_dict)


@router.post("/jobs", description="新增岗位")
async def add_job(job: JobInSchema):
    await Job.create(**job.dict())
    return APIResponse(msg='岗位新增成功')


@router.delete("/job", description="删除岗位")
async def delete_job(ids: List[int]):
    print("ids",ids)
    await Job.filter(id__in=ids).update(is_delete=True)
    return APIResponse(msg="删除岗位成功")


@router.get("/job/{job_id}", description='查询一个岗位')
async def get_job(job_id: int):
    job = await Job.filter(id=job_id).first()
    job_dict = JobOutSchema.from_orm(job).dict()
    return APIResponse(msg='查询成功', result=job_dict)


@router.put("/job/{job_id}", description="修改一个岗位")
async def put_job(job_id: int, job: JobInSchema):
    await Job.filter(id=job_id).update(**job.dict())
    return APIResponse(msg="修改成功")
